<template>
  <div class="goodsadd">
      <el-steps :active="active" finish-status="success">
        <el-step title="步骤 1" description="基本信息"></el-step>
        <el-step title="步骤 2" description="商品图片"></el-step>
        <el-step title="步骤 3" description="商品内容"></el-step>
        <el-step title="步骤 4" description="提交内容"></el-step>
      </el-steps>
    <el-tabs tab-position="left"  @tab-click="handleClick" v-model="indextab">
      <el-tab-pane label="用户管理" name="0">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2"
         label-width="100px" class="demo-ruleForm">
          <el-form-item label="商品名称" prop="goods_name" >
            <el-input type="text" v-model="ruleForm2.goods_name" auto-complete="off" placeholder="请输入商品名称"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price"  >
            <el-input type="password" v-model="ruleForm2.goods_price" auto-complete="off" placeholder="请输入商品价格"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight"  >
            <el-input v-model.number="ruleForm2.goods_weight" placeholder="请输入商品重量"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number" >
            <el-input v-model.number="ruleForm2.goods_number" placeholder="请输入商品数量"></el-input>
          </el-form-item>
          <el-form-item label="商品分类">
            <el-cascader
            v-model="ruleForm2.goods_cat"
            :options="options"
            :props="props"
            ></el-cascader>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="商品图片" name="1">
        <el-upload
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"

          :action="$axios.defaults.baseURL + 'upload'"
          :headers="headers"
          name="file"
          list-type="picture-card">
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-tab-pane>
      <el-tab-pane label="商品内容" name="2">
        <quill-editor ref="myTextEditor"
          v-model="ruleForm2.goods_introduce"
          @blur="onEditorBlur($event)"
          @focus="onEditorFocus($event)"
          @ready="onEditorReady($event)">
        </quill-editor>
      </el-tab-pane>
      <el-tab-pane label="提交任务" name="3" class="shorewidth">
        <el-button type="success" @click="submitFined()" class="devenpdent">提交</el-button>
      </el-tab-pane>
      <el-button type="primary" @click="nextSteps()">下一步</el-button>
      <el-tab-pane name="4"></el-tab-pane>
    </el-tabs>

    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return{
      active:0,
      indextab:'0',
      ruleForm2:{
        goods_name:'',
        goods_price	:'',
        goods_number:'',
        goods_weight:'',
        goods_introduce:'',
        pics:[],
        goods_cat: []
      },
      rules2:{},
      options: [],
      props:{
        expandTrigger: 'click',
        value: 'cat_id', // 将来选中时, 选项的value值, 将存到goods_cat数组中去
        label: 'cat_name',
        children: 'children'
      },
      headers: {
        Authorization: localStorage.getItem('token')
      },
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  async created(){
    const res = await this.$axios.get('categories?type=3')
    if (res.data.meta.status === 200) {
      this.options = res.data.data
    } else {
      this.$message.error(meta.msg)
    }
  },
  methods: {
    // 改变进度条
    handleClick(tab, event) {
      this.active=parseInt(tab.index);
    },
    // 点击下一步信息
    nextSteps(){
      this.active++;
      this.indextab=this.active+'';
    },
    // 获取商品分类的信息
    async submitFined(){
      try{
        if(this.ruleForm2.goods_name.trim()==""){
          this.$message.error("产品名称不能为空");
          return;
        }
        const res=await this.$axios({
          url:'goods',
          method:'post',
          data:{
            ...this.ruleForm2,
            goods_cat: this.ruleForm2.goods_cat.join(',')
          }
        })
        if(res.data.meta.status==201){
          this.$message.success(res.data.meta.msg);
          this.$router.push("/goods");
        }else{
          this.$message.error(res.data.meta.msg);
        }
      }catch(e){
        console.log(e.message)
      }
    },
    handleSuccess (response, file, fileList) {
      // console.log(response)
      const { meta, data } = response
      if (meta.status === 200) {
        // 将路径以对象的形式存到 form.pics 中去
        this.ruleForm2.pics.push({
          pic: data.tmp_path
        })
      }
    },
    handleRemove (file, fileList) {
      // 删除的同时, 需要将form.pics中的对应文件名的一项移除
      // 获取需要删除的图片的文件名
      const fileName = file.response.data.tmp_path
      this.ruleForm2.pics = this.ruleForm2.pics.filter(item => item.pic !== fileName)
    },
    handlePictureCardPreview (file) {
      // 将图片文件的路径, 赋值给 dialogImageUrl
      this.dialogImageUrl = file.url
      // 控制对话框的显示
      this.dialogVisible = true
    },
    // 富文本编辑器的内容
    onEditorBlur(editor) {
      console.log('editor blur!', editor)
    },
    onEditorFocus(editor) {
      console.log('editor focus!', editor)
    },
    onEditorReady(editor) {
      console.log('editor ready!', editor)
    },
    onEditorChange({ editor, html, text }) {
      // console.log('editor change!', editor, html, text)
      this.content = html
    }
  }
}
</script>

<style lang="less">
  .goodsadd{
    .quill-editor{
      background:#fff;
      .ql-editor{
        height: 300px;
      }
    }
    .devenpdent{
      height: 40px;
      width: 60px;
      border:1px  solid red;
    }
  }
</style>
<style lang="less" scoped>
  .goodsadd{
    width: 100%;
    height: 400px;
  }
</style>